package com.zmn.plat.admin.controller.service.item;

import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.plat.business.interfaces.service.itempart.ItemPartBService;
import com.zmn.plat.common.exception.PlatException;
import com.zmn.plat.model.entity.service.item.ServiceItem;
import com.zmn.plat.model.entity.service.item.ServiceItemEditDIO;
import com.zmn.plat.model.entity.service.itempart.ItemPartEditDIO;
import com.zmn.plat.model.entity.warranty.info.WarrantyInfo;
import com.zmn.plat.model.vo.warranty.WarrantyInfoVO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 故障分组项目表
 *
 * @author tanbiao
 * @since 2019-11-21 18:57:24
 */
@RestController
@RequestMapping("/service/item/")
public class ServiceItemModifyController extends ServiceItemBaseController {

    @Resource
    ItemPartBService itemPartBService;
    /**
     * 新增修改
     *
     * @param faultItem
     * @author tanbiao
     * @since 2019-11-21 18:57:24
     */
    @RequestMapping(value = "addEdit")
    @RequiresPermissions("/service/item/detail.action")
    public ResponseDTO addEdit(HttpServletRequest request, @RequestBody ServiceItemEditDIO faultItem) {
        try {
            faultItemBService.addEdit(faultItem, getMcStaffRealName());
            return ResponseDTO.success(Integer.toString(faultItem.getItemId()), "操作成功！");
        } catch (org.springframework.dao.DuplicateKeyException ex) {
            logger.error("不能重复添加！", faultItem.getItemName());
            return ResponseDTO.fail("数据已经存在，不能重复添加！");
        } catch (PlatException ex) {
            logger.error("数据异常！", ex);
            return ResponseDTO.fail(ex.getMessage());
        } catch (Exception ex) {
            logger.error("新增/修改故障分组项目表失败", ex);
            return ResponseDTO.fail("操作失败！");
        }
    }

    /**
     * 修改排序
     *
     * @param id
     * @param sort
     * @author tanbiao
     * @since 2019-11-21 18:57:24
     */
    @RequestMapping(value = "updateSort")
    @RequiresPermissions("/service/item/detail.action")
    public ResponseDTO updateSort(HttpServletRequest request, @RequestParam(value = "id", defaultValue = "0") Integer id,
                                  @RequestParam(value = "sort", defaultValue = "0") Integer sort) {

        if (id == 0) {
            return ResponseDTO.fail("请选择要更改排序的数据！");
        }
        if (sort == 0) {
            return ResponseDTO.fail("请设置排序值！");
        }

        Integer result = faultItemService.updateSortByKey(id, sort, getMcStaffRealName());
        return ResponseDTO.success(result, "操作成功！");
    }

    @RequestMapping(value = "deleteItem")
    @RequiresPermissions("/service/item/deleteItem.action")
    public ResponseDTO deleteItem(HttpServletRequest request, @RequestParam(value = "itemId", defaultValue = "0") Integer itemId) {
        Integer result = faultItemBService.deleteByKey(itemId);
        return ResponseDTO.success(result, "操作成功！");
    }

    @RequestMapping(value = "addEditWarrantyInfo")
    @RequiresPermissions("/service/item/addEditWarrantyInfo.action")
    public ResponseDTO addEditWarrantyInfo(HttpServletRequest request, @RequestBody WarrantyInfo warrantyInfo) {
        String operator = getMcStaffRealName();
        Date now = DateUtil.getNow();
        try {
            WarrantyInfo dbResult = warrantyInfoService.findByItemIdAndCategIdAndBizType(warrantyInfo.getItemId(),
                    warrantyInfo.getCategId(), warrantyInfo.getBizType());
            if (dbResult != null) {
                warrantyInfo.setWarrantyInfoId(dbResult.getWarrantyInfoId());
            }

            if (warrantyInfo.getWarrantyInfoId() == null || warrantyInfo.getWarrantyInfoId() == 0) {
                warrantyInfo.setCreater(operator);
                warrantyInfo.setCreateTime(now);
                warrantyInfo.setUpdater(operator);
                warrantyInfo.setUpdateTime(now);
                warrantyInfoService.insert(warrantyInfo);
            } else {
                warrantyInfo.setUpdater(operator);
                warrantyInfo.setUpdateTime(now);
                warrantyInfoService.updateByKey(warrantyInfo);
            }
            return ResponseDTO.success(Integer.toString(warrantyInfo.getWarrantyInfoId()), "操作成功！");
        } catch (Exception ex) {
            logger.error("新增/修改保修信息失败", ex);
            return ResponseDTO.fail("操作失败！");
        }
    }

    @RequestMapping(value = "batchAddEditWarrantyInfo")
    @RequiresPermissions("/service/item/batchAddEditWarrantyInfo.action")
    public ResponseDTO batchAddEditWarrantyInfo(HttpServletRequest request, @RequestBody WarrantyInfoVO warrantyInfoVO) {
        String operator = getMcStaffRealName();
        try {
            faultItemBService.batchAddEditWarrantyInfo(warrantyInfoVO, operator);
            return ResponseDTO.success(warrantyInfoVO.getItemIds(), "操作成功！");
        } catch (Exception ex) {
            logger.error("快速设置保修信息失败", ex);
            return ResponseDTO.fail("操作失败！");
        }
    }

    @RequestMapping(value = "addEditServiceMinutes")
    @RequiresPermissions("/service/item/addEditServiceMinutes.action")
    public ResponseDTO addEditServiceMinutes(@RequestParam(value = "itemId", defaultValue = "0") Integer itemId,
                                             @RequestParam(value = "serviceMinutes", defaultValue = "0") Integer serviceMinutes) {
        String operator = getMcStaffRealName();
        if (NumberUtil.isNullOrZero(itemId)) {
            return ResponseDTO.fail("故障项目ID不能为空！");
        }
        try {
            ServiceItem item = faultItemService.findByKey(itemId);
            item.setUpdater(operator);
            item.setUpdateTime(DateUtil.getNow());
            item.setServiceMinutes(NumberUtil.isNullOrZero(serviceMinutes) ? null : serviceMinutes);
            faultItemService.updateByKey(item);
            return ResponseDTO.success(itemId, "操作成功！");
        } catch (Exception ex) {
            logger.error("通用服务时长设置失败", ex);
            return ResponseDTO.fail("操作失败！");
        }
    }

    @RequestMapping(value = "addEditPart")
    @RequiresPermissions("/service/item/addEditPart.action")
    public ResponseDTO addEditPart(@RequestBody ItemPartEditDIO dio) {
        String operator = getMcStaffRealName();
        if (NumberUtil.isNullOrZero(dio.getItemId())) {
            return ResponseDTO.fail("故障项目ID不能为空！");
        }
        try {
            dio.setOperator(operator);
            itemPartBService.setPartRelItem(dio);
            return ResponseDTO.success(dio.getItemId(), "操作成功！");
        } catch (Exception ex) {
            logger.error("设置关联配件失败", ex);
            return ResponseDTO.fail("操作失败！");
        }
    }

}
